package db

import (
	"gitee.com/Caomao/cmgo/log4g"
	"gopkg.in/mgo.v2"
	"sync/atomic"
	"time"
)

type MongoBDManager struct {
	DbSession *mgo.Session
	DbList    chan *mgo.Session
	used      int32
}

func NewMongoBD(url string, poolSize int, release bool) *MongoBDManager {
	db := MongoBDManager{}
	db.Connect(url, release)
	db.NewSessionPool(poolSize)
	return &db
}
func (db *MongoBDManager) NewSessionPool(size int) {
	if db.DbSession != nil {
		db.DbList = make(chan *mgo.Session, size)
		db.used = 0
	}
}
func (db *MongoBDManager) Connect(url string, release bool) {
	var err error
	dialInfo := &mgo.DialInfo{
		Addrs:     []string{"127.0.0.1"},
		Direct:    false,
		Timeout:   time.Second * 2,
		Database:  "sanguozhengba",
		Source:    "sanguozhengba",
		Username:  "chenfuling",
		Password:  "cfl19950309",
		PoolLimit: 4096, // Session.SetPoolLimit
	}
	if release {
		db.DbSession, err = mgo.DialWithInfo(dialInfo)
	} else {
		db.DbSession, err = mgo.Dial(url)
	}
	if err != nil {
		log4g.Error(err.Error())
	}
	db.DbSession.SetMode(mgo.Eventual, true)
}
func (db *MongoBDManager) Get() *mgo.Session {
	atomic.AddInt32(&db.used, 1)
	select {
	case session, more := <-db.DbList:
		if session == nil || !more {
			log4g.Error("MongoDBPool已经关闭!")
		}
		return session
	default:
		return db.DbSession.Copy()
	}
}
func (db *MongoBDManager) Put(session *mgo.Session) {
	atomic.AddInt32(&db.used, -1)
	if session == nil {
		return
	}
	select {
	case db.DbList <- session:
	default:
		//如果db.DbList满了的话，就关闭
		session.Close()
	}
}
func (db *MongoBDManager) Close() {
	close(db.DbList)
	for session := range db.DbList {
		session.Close()
	}
	db.DbSession.Close()
}
func (db *MongoBDManager) Used() int {
	return int(db.used)
}
